import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
	pageCur:'index',
	userInfo: {},
	hasLogin: false,
	webUrl:'http://192.168.0.153/',
	resourceUrl:'http://127.0.0.1:8080/resource'
}
const actions = {
	NavChanges(context,args){
		context.commit('NAVCHANGES',args)
	}
}
const mutations = {
	TEST(state){
		state.hasLogin = !state.hasLogin
	},
	login(state,payload) {
		state.hasLogin = true
		state.userInfo.id = payload.id
		state.userInfo.userToken = payload.userToken
		state.userInfo.username = payload.username
		state.userInfo.nickname = payload.nickname
		state.userInfo.stars_counts = payload.stars_counts
		state.userInfo.u_background_path = payload.u_background_path
		state.userInfo.u_face_image_path = payload.u_face_image_path
		state.userInfo.fans_counts = payload.fans_counts
		state.userInfo.follow_counts = payload.follow_counts
		state.userInfo.u_profile = payload.u_profile
		uni.setStorage({
			key:'userInfo',
			data:payload
		})
	},
	logout(state,payload) {
		console.log("到达logout前:userInfo====>"+JSON.stringify(state.userInfo)+"====>"+state.hasLogin)
		state.hasLogin = false
		state.userInfo.id = null
		state.userInfo.userToken = null
		state.userInfo.username = null
		state.userInfo.nickname = null
		state.userInfo.stars_counts = null
		state.userInfo.u_background_path = null
		state.userInfo.u_face_image_path = null
		state.userInfo.fans_counts = null
		state.userInfo.follow_counts = null
		state.userInfo.u_profile = null
		console.log("到达logout后:userInfo====>"+JSON.stringify(state.userInfo)+"====>"+state.hasLogin)
		uni.request({
			url:state.webUrl+'logout?userid='+payload,
			success(res) {
				console.log("res===>"+JSON.stringify(res.data))
			}
		})
		uni.removeStorage({
			key: 'uerInfo'
		});
	},
	NAVCHANGES(state,payload) {
		state.pageCur = payload
	},
	changeBGI(state,payload){
		state.userInfo.u_background_path = payload
		uni.getStorage({
			key:'userInfo',
			success: (res) => {
				var obj = res.data
				obj.u_background_path = payload
				uni.setStorage({
					key:'userInfo',
					data:obj
				})
			}
		})
	},
	changeFAI(state,payload){
		state.userInfo.u_face_image_path = payload
		uni.getStorage({
			key:'userInfo',
			success: (res) => {
				var obj = res.data
				obj.u_face_image_path = payload
				uni.setStorage({
					key:'userInfo',
					data:obj
				})
			}
		})
	}
}
const store = new Vuex.Store({
	state,
	actions,
	mutations
})

export default store